package 手写排序;

import java.util.Arrays;
import java.util.Random;

public class Sort {

    /**
     * 归并排序
     * 时间复杂度：O(nlog(n))(不考虑优化)
     * 空间复杂度：O(n)
     * 稳定性:稳定
     */
    public static int[] mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    return array;
    }
    private static void mergeSortFunc(int[] array, int left,int right ) {
        if(left >= right) {
            return;
        }
        //找到中间位置
        System.out.println();
        System.out.println();
        System.out.println();
        int mid = left+(right-left)/2;
        //一直递归，递归到只剩两个结点开始归并
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left, right,mid);

    }
    private static void merge(int[] array,int start,int end,int mid) {
        int s1 = start;
        int s2 = mid+1;
        int size = 0;
        int []ret =new int[end-start+1];
        while (s1 <= mid && s2 <= end) {
            if(array[s1] < array[s2]) {
                ret[size++] = array[s1++];
            }else {
                ret[size++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            ret[size++] = array[s1++];
        }
        while (s2 <= end) {
            ret[size++] = array[s2++];
        }
        System.arraycopy(ret, 0, array, start, ret.length);
    }

        /**
         * 堆排序
         * 时间复杂度O(nlog(N))
         * 空间复杂度O(1)不需要额外开辟空间
         * 不稳定
         */
    public static int[] HeapSort(int[] array) {
        createBigHeap(array);
        HeapSort(array,0,array.length-1);
        return array;
    }
    public static void HeapSort(int[] array ,int left ,int right) {

        int size = array.length-1;
        while (size > 0) {
            swap(array,0,size);
            //向下调整
            shiftDown(array,0,size);
            size--;
        }

    }
    //建堆
    public static void createBigHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >=0 ; i--) {
            shiftDown(array,i,array.length);
        }
    }
    private static void shiftDown(int[] array ,int parent,int end) {
        int child = parent*2+1;
        //升序建立大堆
        while (child < end) {
            if(child+1 < end && array[child+1] > array [child]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
            }else {
                return;
            }
            parent=child;
            child=child*2+1;
        }
    }
    /**
     * 选择排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void selectSort(int []array) {
        for (int i = 0; i <array.length ; i++) {
            //找到最小值的下标
            int minIndex=i;
            for (int j = i+1; j < array.length; j++) {
                if(array[minIndex]>array[j]) {
                    //记录最小值的下标
                    minIndex=j;
                }
            }
            //最小值和i交换
            swap(array,i,minIndex);
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：平均时间复杂度为O（n^1.3）
     * 空间复杂度：没有开辟额外空间空间复杂度:O(1)
     * 稳定性：不稳定
     */
    public  static int[] ShellSort(int []array) {
        int gap = array.length;
        while (gap > 1) {
            gap/=2;
            InsertSort2(array,gap);
        }
        InsertSort(array,0,array.length-1);
        return array;
    }
    public static void InsertSort2(int[]array,int gap) {
        for (int i = gap; i <array.length ; i++) {
            int j = i-gap;
            int tmp = array[i];
            for (; j >=0 ; j-=gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }


        /**
         * 插入排序
         * 时间复杂度：最坏情况O(n^2) 最好情况下是o(n)有序条件下排序很快
         * 空间复杂度：没有开辟额外空间空间复杂度:O(1)
         * 稳定性：稳定
         */
    public static void InsertSort1(int[]array,int left,int right) {
        for (int i = 0; i <=right ; i++) {
            int tmp =array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }


        /**
         * 插入排序
         */
    public static void InsertSort(int[]array,int left,int right) {
        for (int i = left+1; i <=right ; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

        /**
         * 快速排序
         * 时间复杂度：最坏情况O(n^2) 优化后可以达到O(nlogN)
         * 空间复杂度：由于是递归，最坏情况下递归n层，空间复杂度:O(N)
         * 最好情况下空间复杂度是O(logN)
         * 优化方法：三数取中-》小区间插入法
         * 稳定性：不稳定
         * @param array
         * @return
         */
    public static int[] quickSort(int[] array) {
        //手写快排
        //
        quickSort(array,0,array.length-1);
        return array;
    }
    public static void quickSort(int[] array ,int left, int right ) {
        //
        if(left >= right) {
            return;
        }
        if(right-left+1<=3) {
            InsertSort(array,left,right);
            return;
        }
        //三数取中法
        int mid = ThreeMiddle(array,left,right);
        swap(array,left,mid);
        //找基准
       int index = partition1(array,left,right);
       quickSort(array,left,index-1);
       quickSort(array,index+1,right);

    }
    //找基准
    //Hoare法
    private static int partition1(int[] array,int left, int right) {
        int tmp = array[left];
        int n = left;
        while (left<right) {
            while (left<right && array[right] >= tmp) {
                right--;
            }
            while (left<right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,n,left);
        return left;
    }
    //三数取中
    private static int ThreeMiddle(int[] array,int left,int right) {
        int mid = left + (right-left)/2;
        if(array[left] > array[right]) {
            if(array[right]>array[mid]) {
                return right;
            }
            if(array[left] > array[mid]) {
                return mid;
            }else {
                return left;
            }
        }else {
            if(array[left]>array[mid]) {
                return left;
            }
            if(array[right]>array[mid]) {
                return mid;
            }else {
                return right;
            }
        }
    }
    private static void swap(int[] array ,int left,int right) {
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }
    //堆排序测试
    public static void testHeapSort(int []array) {
        int[]copy=Arrays.copyOf(array,array.length);
        //排序.Sort.createBigHeap(copy);
        long start = System.currentTimeMillis();
        HeapSort(copy);
        long end = System.currentTimeMillis();
        System.out.println("堆排序耗时"+(end-start));

    }
    private static void testQuickSort(int[]array) {
        int[]copy = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        copy = quickSort(copy);
        long end = System.currentTimeMillis();
        System.out.println("快速耗时"+(end-start));

    }
    public static void testShellSort(int []array) {
        int[]copy=Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        ShellSort(copy);
        long end = System.currentTimeMillis();
        System.out.println("希尔耗时"+(end-start));

    }
    public static void initArrayNotOrder(int []array) {
        Random random=new Random();
        for (int i = 0; i < array.length; i++) {
            array[i]=random.nextInt(1000_0000);
        }
        System.out.println("无序:");

    }

    public static void main(String[] args) {
        int[]array=new int[10_0000];
        initArrayNotOrder(array);
        testQuickSort(array);
        testShellSort(array);
        int[]ret={12,56,32,67,10,19,4};
        ret =  mergeSort(ret);
        System.out.println(Arrays.toString(ret));
    }
}
